AddCSLuaFile();

ENT.Type = "anim";
ENT.PrintName = "Tree";
ENT.Author = "Dutchfreak";
ENT.Spawnable = true;
ENT.Information	= "Soon to be a turtle fractal tree";
ENT.Category = "Darwinia";

if (SERVER) then
	resource.AddFile("materials/entities/sent_plandle.lua");
end;

function ENT:SpawnFunction(client, trace)
	if (!trace.Hit) then
		return;
	end;

	local entity = ents.Create(self.ClassName);
	entity:SetPos(trace.HitPos + trace.HitNormal * 32);
	entity:SetOwner(client);
	entity:Spawn();
	entity:Activate();

	return entity;
end;

function ENT:Initialize()
	if (SERVER) then
		self:SetModel("models/props_junk/terracotta01.mdl");
		self:SetSolid(SOLID_VPHYSICS);
		self:SetMoveType(MOVETYPE_VPHYSICS);
		self:PhysicsInit(SOLID_VPHYSICS);
		self:SetUseType(SIMPLE_USE);

		local physicsObject = self:GetPhysicsObject();

		if ( IsValid(physicsObject) ) then
			physicsObject:Wake();
			physicsObject:EnableMotion(true);
		end;
	else
		local currentMin, currentMax = self:GetRenderBounds();
		local min = currentMin + Vector(0, 0, 128);
		local max = currentMax;

		self:SetRenderBounds(min, max);
	end;
end;

if (CLIENT) then
	ENT.DeltaVolume = 8;

	local height = 0.5;

	local render = render;
	local drawLine = render.DrawLine;

	function ENT:DrawTree(x, y, angle, depth, z)
		if (depth <= 0) then
			return;
		end;

		local x2 = x + math.cos(math.rad(angle)) * depth^2 * height;
		local y2 = y + math.sin(math.rad(angle)) * depth^2 * height;

		local position = self:GetPos();
		local angles = self:GetAngles();
		local up, right, forward = angles:Up(), angles:Right(), angles:Forward();
		local fraction = self.DeltaVolume / 50;
		local color = Color( (1 - fraction) * 255, fraction * 255, 0 );

		drawLine(position + up*y + right*x + forward*z, position + up*y2 + right*x2 + forward*z2, color, true);
		
		self:DrawTree(x2, y2, angle - 15 , depth - 1);
		self:DrawTree(x2, y2, angle + 15 , depth - 1);
	end;

	function ENT:Draw()
		--self:DrawModel();

		if ( IsValid( self:GetOwner() ) ) then
			local position = self:GetPos();
			local volume = (0.6 * 64) + 8;
			local angles = self:GetAngles();
			local x, y = math.sin(RealTime() * 0.3) * 0.5, math.abs(math.sin(RealTime() * 0.4) * 0.8);

			self.DeltaVolume = math.Approach(self.DeltaVolume, volume, FrameTime() * 60);

			local height = ( (self.DeltaVolume / 5.5) + 2 + math.cos(RealTime() * 0.02) ) / 1.25;

			self:DrawTree(x, y, 90, height);
		end;
	end;

	function ENT:Think()
		local fraction = self.DeltaVolume / 50;
		local light = DynamicLight( self:EntIndex() );

		if (light) then
			local r, g, b = (1 - fraction) * 255, fraction * 255, 0;

			light.Pos = self:GetPos() + Vector(0, 0, 16);
			light.r = r;
			light.g = g;
			light.b = b;
			light.Brightness = fraction * 2;
			light.Size = (fraction * 256);
			light.Decay = (fraction * 256) * 8;
			light.DieTime = CurTime() + 1;
			light.Style = 5;
		end;
	end;
end;